// SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
// SPDX-License-Identifier: Apache-2.0

package roadmap_dao

import (
	"context"

	"gitcode.com/lvupclub/lvup-api/dao/common"
	"gitcode.com/lvupclub/proto-gen-go.git/src/roadmap_data_api"
	"gitcode.com/lvupclub/proto-gen-go.git/src/roadmap_data_type_api"
	"go.mongodb.org/mongo-driver/v2/bson"
	"go.mongodb.org/mongo-driver/v2/mongo"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/known/anypb"
)

type NodeInfoDbItem struct {
	NodeId      string  `bson:"_id"`
	RoadmapId   string  `bson:"roadmapId"`
	X           float32 `bson:"x"`
	Y           float32 `bson:"y"`
	W           float32 `bson:"w"`
	H           float32 `bson:"h"`
	ContentData []byte  `bson:"contentData"`
	CreateTime  int64   `bson:"createTime"`
	UpdateTime  int64   `bson:"updateTime"`
}

func (item *NodeInfoDbItem) ToNodeInfo() (*roadmap_data_api.NodeInfo, error) {
	content := &anypb.Any{}
	err := proto.Unmarshal(item.ContentData, content)
	if err != nil {
		content, err = anypb.New(&roadmap_data_type_api.UnknownData{})
		if err != nil {
			return nil, err
		}
	}
	return &roadmap_data_api.NodeInfo{
		NodeId:     item.NodeId,
		X:          item.X,
		Y:          item.Y,
		W:          item.W,
		H:          item.H,
		Content:    content,
		CreateTime: item.CreateTime,
		UpdateTime: item.UpdateTime,
	}, nil
}

type _NodeInfoDao struct {
	mc *mongo.Collection
}

func newNodeInfoDao(db *mongo.Database) (*_NodeInfoDao, error) {
	mc := db.Collection(common.DB_NS_ROADMAP + "node")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"roadmapId", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_NodeInfoDao{mc: mc}, nil
}

func (db *_NodeInfoDao) Insert(ctx context.Context, dbItem *NodeInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_NodeInfoDao) Exist(ctx context.Context, roadmapId, nodeId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": nodeId, "roadmapId": roadmapId})
	return count > 0, err
}

func (db *_NodeInfoDao) Get(ctx context.Context, roadmapId, nodeId string) (*NodeInfoDbItem, error) {
	retItem := &NodeInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": nodeId, "roadmapId": roadmapId}).Decode(retItem)
	return retItem, err
}

func (db *_NodeInfoDao) Remove(ctx context.Context, roadmapId, nodeId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": nodeId, "roadmapId": roadmapId})
	return err
}

func (db *_NodeInfoDao) RemoveByRoadmap(ctx context.Context, roadmapId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"roadmapId": roadmapId})
	return err
}

func (db *_NodeInfoDao) List(ctx context.Context, roadmapId string) ([]*NodeInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"roadmapId": roadmapId})
	if err != nil {
		return nil, err
	}
	retList := []*NodeInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_NodeInfoDao) UpdatePosition(ctx context.Context, roadmapId, nodeId string, x, y float32, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": nodeId, "roadmapId": roadmapId}, bson.M{
		"$set": bson.M{
			"x":          x,
			"y":          y,
			"updateTime": updateTime,
		}})
	return err
}

func (db *_NodeInfoDao) UpdateSize(ctx context.Context, roadmapId, nodeId string, w, h float32, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": nodeId, "roadmapId": roadmapId}, bson.M{
		"$set": bson.M{
			"w":          w,
			"h":          h,
			"updateTime": updateTime,
		}})
	return err
}

func (db *_NodeInfoDao) UpdateData(ctx context.Context, roadmapId, nodeId string, contentData []byte, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": nodeId, "roadmapId": roadmapId}, bson.M{
		"$set": bson.M{
			"contentData": contentData,
			"updateTime":  updateTime,
		}})
	return err
}
